package com.umeox.watch.moto.pedometer.base;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.common.utils.DateUtils;
import com.umeox.watch.moto.dataservice.constants.Constants;
import com.umeox.watch.moto.pedometer.util.CacheImpl;
import com.umeox.watch.moto.pedometer.util.ICache;
import com.umeox.watch.moto.pedometer.util.LogUtils;
import com.umeox.watch.moto.pedometer.util.StepManager;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;

public class PedometerReceiver extends BroadcastReceiver {
    private static final String TAG = "PedometerReceiver";

    @Override
    public void onReceive(final Context context, final Intent intent) {
        String action = intent.getAction();
        if (action == null) {
            return;
        }
        Logger.e(TAG, "action:" + action);
        LogUtils.saveLog("-->action:" + action);
        final StepManager stepManager = StepManager.getInstance();
        final ICache cache = CacheImpl.getInstance();
        final int todayMillis = DateUtils.getTodayMillis();
        switch (action) {
            case Constants.ACTION_UNBIND:
                stepManager.resetSensorFlag();
                PedometerDBHelper.getInstance().removeALlEntries();
                CacheImpl.getInstance().clearAll();
                cache.remove(ICache.KEY_STEP_COUNT_TOTAL);
                break;

            case Constants.ACTION_ACTIVATION:
                PedometerDBHelper.getInstance().removeALlEntries();
                stepManager.resetSensorFlag();
                cache.remove(ICache.KEY_STEP_COUNT_TOTAL);
                break;
            case StepManager.ACTION_PEDOMETER_RESET:
                Logger.e(TAG, "ACTION_PEDOMETER_RESET");
                LogUtils.saveLog("-->ACTION_PEDOMETER_RESET");
                EventBus.getDefault().post(new PedometerDeviceEvent(PedometerDeviceEvent.Action.RESET));
                break;
            case Intent.ACTION_SHUTDOWN:
            case Intent.ACTION_REBOOT:
            case Constants.ACTION_REMOTE_REBOOT:
                EventBus.getDefault().post(new PedometerDeviceEvent(PedometerDeviceEvent.Action.SHUTDOWN));
                break;
            case Intent.ACTION_BOOT_COMPLETED:
                int bootCacheTime = cache.get(ICache.KEY_STEP_TIME, 0);
                Logger.e(TAG, "bootCacheTime=" + bootCacheTime + ",todayMillis:" + todayMillis);
                LogUtils.saveLog("-->bootCacheTime=" + bootCacheTime + ",todayMillis:" + todayMillis);
                if (bootCacheTime != todayMillis) {
                    EventBus.getDefault().postSticky(new PedometerDeviceEvent(PedometerDeviceEvent.Action.BOOT));
                }
                setAlarmTime(context);
                break;
        }
    }

    private void setAlarmTime(Context context) {
        Logger.e(TAG, "setAlarmTime");
        LogUtils.saveLog("-->setAlarmTime");
        Intent intent = new Intent(context, PedometerReceiver.class);
        intent.setAction(StepManager.ACTION_PEDOMETER_RESET);
        AlarmManager alm = ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 5, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        alm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);

    }
}
